#pragma once
#include <iostream>
#include <string>
#include <cstdlib>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Logger.hpp"
#include "Common.hpp"
#include "Inet_host.hpp"

namespace SocketModule
{
    using namespace LogMudule;
    class Socket;
    using sockptr = std::shared_ptr<Socket>;
    class Socket
    {
    public:
        virtual ~Socket() = default;
        virtual void SocketCreate() = 0;
        virtual void SetSocketOption() = 0;
        virtual bool SocketBind(int port) = 0;
        virtual bool SocketListen() = 0;
        virtual int Accept(InetAddr *client) = 0;
        virtual void Close() = 0;
        virtual int Rec(std::string *out) = 0;
        virtual int Send(const std::string &in) = 0;
        virtual int Fd() = 0;

        // 提供固定创建socket的接口
        void BuildTcpSocket(int port)
        {
            SocketCreate();
            SetSocketOption();
            SocketBind(port);
            SocketListen();
        }
    };

    class TcpSocket : public Socket
    {
    public:
        TcpSocket():_sockfd(defaultfd)
        {}
        TcpSocket(int sockfd):_sockfd(sockfd)
        {}
        ~TcpSocket()
        {}
        virtual void SocketCreate() override
        {
            _sockfd = socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd == defaultfd)
            {
                LOG(LogLevel::ERROR) << "socket create error";
                Die(SOCKET_ERR);
            }
        }
        virtual void SetSocketOption() override
        {
        }
        virtual bool SocketBind(int port) override
        {
            if(_sockfd == defaultfd)
            {
                return false;
            }
            InetAddr addr(port);
            int n = ::bind(_sockfd, addr.SockAddr(), addr.SockLen());
            if (n < 0)
            {
                LOG(LogLevel::ERROR) << "bind error";
                return false;
            }
            LOG(LogLevel::INFO) << "bind success";
            return true;
        }
        virtual bool SocketListen() override
        {
            if(_sockfd == defaultfd)
            {
                return false;
            }
            int n = ::listen(_sockfd, gbacklog);
            if (n < 0)
            {
                LOG(LogLevel::ERROR) << "listen error";
                return false;
            }
            LOG(LogLevel::INFO) << "listen success";
            return true;
        }
        virtual int Accept(InetAddr *client) override
        {
            if(client == nullptr)
            {
                return -1;
            }
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int newsockfd = ::accept(_sockfd, Conv(&peer), &len);
            if (newsockfd < 0)
            {
                LOG(LogLevel::WARNING) << "accept error";
                return -1;
            }
            return newsockfd;  
        }
        virtual void Close() override
        {
            if (_sockfd != defaultfd)
            {
                ::close(_sockfd);
                _sockfd = defaultfd;
            }
        }
        virtual int Rec(std::string *out) override
        {
            if (out == nullptr)
            {
                return -1;
            }
            char buffer[1024*8];
            int size = ::recv(_sockfd, buffer, sizeof(buffer)-1, 0);
            if (size > 0)
            {
                buffer[size] = '\0';
                LOG(LogLevel::INFO) << "recv success";
                *out = buffer;
            }
            return size;
        }
        virtual int Send(const std::string &in) override
        {
            auto size = ::send(_sockfd, in.c_str(), in.size(), 0);
            return size;
        }
        virtual int Fd() override
        {
            return _sockfd;
        }
    private:
        int _sockfd;
    };
}
